<?php

class Webkul_Marketplacepartner_ProductsController extends Mage_Adminhtml_Controller_Action
{
	public function indexAction(){
        if ($this->getRequest()->getQuery('ajax')){
            $this->_forward('grid');
            return;
        }
        $this->loadLayout();
        //Set active menu item
        $this->_setActiveMenu('marketplacepartner');
        //Append freebidspromo block to content
        $this->_addContent($this->getLayout()->createBlock('marketplacepartner/products', 'products'));
        $this->renderLayout();
	}
		
    public function gridAction(){
        $this->getResponse()->setBody($this->getLayout()->createBlock('marketplacepartner/products_grid')->toHtml());
    }

	public function ltp(){
        $data = array();
		$data['name'] = 'SI!';
		$data['description'] = 'ASDAasdsad asd ad .arcu quam commodo nisl, vel tincidunt massa orci eget nisi. Quisque vehicula enim. Praesent sodales lectus eget lectus. Duis vel l';
		$data['short_description'] = 'Qasdasdmsa oinadfi dns. Nam nisl tellus, dapibus in; elementum ac, sollicitudin sed';
		$data['price'] = 20;
		$data['stock'] = 45;
		$data['weight'] = 0.5;
		$data['customer_id'] = 1;
        Mage::getModel('marketplacepartner/marketplacepartner')->createPartnerProduct($data);
	}

	//Method to approve a customer uploaded product.
	//Basically, creates a Magento Store product with the data entered by the Customer.
	
	
	public function approveAction(){
		$id = (int)$this->getRequest()->getParam('id');
  		//If we do not have the ID of the product, redirect GRID
		if(!$id){$this->_redirectReferer();}
		
		$lastId=Mage::getModel('marketplacepartner/userdata')->approveSimpleProduct($id);
		Mage::getSingleton('adminhtml/session')->addSuccess(Mage::helper('marketplacepartner')->__('Product successfully approved.'));
		$this->_redirect('adminhtml/catalog_product/edit', array('id' => $lastId,'_current'=>true));
		
	}

	//Clear a product owed amount
	public function clearoAction(){	
		$id = (int)$this->getRequest()->getParam('id');
		if(!$id){$this->$this->_redirectReferer();}
		try{
			//mysql prefix checking and execute code
			$query = sprintf('UPDATE "'.Mage::getConfig()->getTablePrefix().'"marketplacepartner_aux SET amount_owed = 0, cleared_at = NOW() WHERE product_id = %d AND amount_owed > 0', $id);
			Mage::getModel('marketplacepartner/marketplacepartner')->customQuery($query, 'write');
			Mage::getSingleton('adminhtml/session')->addSuccess(Mage::helper('marketplacepartner')->__('Amount owed successfully cleared.'));
			$this->_redirectReferer();
		}catch(Exception $e){
			Mage::getSingleton('adminhtml/session')->addError($e->getMessage());
			$this->_redirectReferer();
		}
	}
}
